home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11174 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Message-ID: <110310Z22031996@anon.penet.fi>
  2. Path: news.eunet.fi!anon.penet.fi
  3. Newsgroups: comp.lang.c
  4. From: an25305@anon.penet.fi (Cdr. Coriander Salamander)
  5. X-Anonymously-To: comp.lang.c
  6. Organization: Anonymous forwarding service
  7. Reply-To: an25305@anon.penet.fi
  8. Date: Fri, 22 Mar 1996 10:53:11 UTC
  9. Subject: Re: rand ?
  10. References: <4idjt4$4du@itsop2.its.brooklyn.cuny.edu> <314BB352.2734@ccis.com>
  11.  
  12. In article <314BB352.2734@ccis.com>, Derek Lund <wlund@ccis.com> writes:
  13. > #define RANDOMIZE() srand(time(NULL)) /* time() is in time.h */
  14.  
  15. This may fail on machines without prototypes.  Safer is
  16. srand((unsigned int)time((time_t *)NULL)) .
  17.  
  18. >  int random(int i)
  19. >  {
  20. >   double x = RAND_MAX + 1.0 ;/* RAND_MAX is defined in stdlib.h */
  21. >   int y ; 
  22. >   RANDOMIZE();
  23. >   y = 1 + rand() * (i /x) ;
  24. >   return y ;
  25. >  }
  26.  
  27. It's a *horrible* idea to call srand() (or anything which calls it) once
  28. for each time you call rand(). You won't get "more random" numbers this
  29. way, in fact you'll get less random. If you call this random() five
  30. times in one second, you'll get the same number back each time.
  31.  
  32. Since you should typically call srand() just once, there's hardly any
  33. need to encapsulate it in a macro.
  34.  
  35. --****ATTENTION****--****ATTENTION****--****ATTENTION****--***ATTENTION***
  36. Your e-mail reply to this message WILL be *automatically* ANONYMIZED.
  37. Please, report inappropriate use to                abuse@anon.penet.fi
  38. For information (incl. non-anon reply) write to    help@anon.penet.fi
  39. If you have any problems, address them to          admin@anon.penet.fi
  40.